#include<iostream>
#include<unistd.h>
#include<pthread.h>
using namespace std;
#include"RingQueue.hpp"
#include"task.hpp"
#include<ctime>

void *consumer(void * arg)
{
    RingQueue<task>* rq = static_cast<RingQueue<task>*>(arg);
    while(true)
    {
        task t;
        rq->pop(&t);
        t.run();
        cout<<t.ConsumerMes()<<endl;
    }

    return nullptr;
}

void *productor(void* arg)
{
    RingQueue<task>* rq = static_cast<RingQueue<task>*>(arg);
    
    while(true)
    {
        int x = rand()%10;
        usleep(rand()%213);
        int y = rand()%10;
        usleep(rand()%213);
        char oper = opers[rand()%opers.size()];

        task t(x,y,oper);
        rq->push(t);
        cout<<t.ProductorMes()<<endl;
    }

    return nullptr;
}

int main()
{
    srand((size_t)pthread_self()&217367^283187);
    // RingQueue<int>* rq = new RingQueue<int>;
    RingQueue<task>* rq = new RingQueue<task>;
    pthread_t c[3],p[2];

    pthread_create(&c[0],nullptr,consumer,rq);
    pthread_create(&c[1],nullptr,consumer,rq);
    pthread_create(&c[2],nullptr,consumer,rq);
    pthread_create(&p[0],nullptr,productor,rq);
    pthread_create(&p[1],nullptr,productor,rq);

    pthread_join(c[0],nullptr);
    pthread_join(c[1],nullptr);
    pthread_join(c[2],nullptr);
    pthread_join(p[0],nullptr);
    pthread_join(p[1],nullptr);

    return 0;
}

